//
//  ByUserIndexLookDoctorVC.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/9.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserIndexLookDoctorVC.h"

// other
#import "ZKSegmentControl.h"

// model
#import "ByModelRegistrationSubject.h"

// view
#import "ByViewLookDoctorSegment.h"
#import "ByViewLookDoctorSearch.h"

// cell
#import "ByCellLookDotor.h"

// vc
#import "ByUserIndexDoctorDetailVC.h"

@interface ByUserIndexLookDoctorVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) ByViewLookDoctorSearch *searchView;
@property (nonatomic, strong) ByViewLookDoctorSegment *segmentView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView      *bgView;
@property (nonatomic, strong) UITableView *hospitalView;    // 分院view
@property (nonatomic, strong) UITableView *subjectView;     // 科室view

@property (nonatomic, strong) NSMutableArray *hospitalAry;  // 分院数据
@property (nonatomic, strong) NSMutableArray *subjectAry;   // 科室数据
@property (nonatomic, strong) NSMutableArray *dataAry;      // 筛选结果

@property (nonatomic, assign) NSInteger selectedIndex;
@end

@implementation ByUserIndexLookDoctorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBarButtonItem];
    self.title = @"金普名医";
    [self createSearchView];
    [self fetchData];
}

#pragma mark - getter
- (NSMutableArray *)hospitalAry{
    if (!_hospitalAry) {
        _hospitalAry = [NSMutableArray array];
    }
    return _hospitalAry;
}

- (NSMutableArray *)subjectAry{
    if (!_subjectAry) {
        _subjectAry = [NSMutableArray array];
    }
    return _subjectAry;
}

- (ByViewLookDoctorSegment *)segmentView{
    if (!_segmentView) {
        _segmentView = [[ByViewLookDoctorSegment alloc] init];
        [self.view addSubview:_segmentView];
        [_segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(0));
            make.top.equalTo(self.searchView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
        }];
    }
    return _segmentView;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        [self.view insertSubview:_bgView aboveSubview:self.tableView];
        _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(0));
            make.top.equalTo(self.segmentView.mas_bottom);
            make.right.equalTo(@(0));
            if (@available(iOS 11.0,*)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            }else{
                make.bottom.equalTo(self.view.mas_bottom);
            }
        }];
        _bgView.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView)];
        _bgView.userInteractionEnabled = YES;
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (UITableView *)hospitalView{
    if (!_hospitalView) {
        _hospitalView = [[UITableView alloc] init];
        [self.view insertSubview:_hospitalView aboveSubview:self.bgView];
        [_hospitalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(0));
            make.top.equalTo(self.segmentView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2, 0));
        }];
        _hospitalView.hidden = YES;
        _hospitalView.delegate = self;
        _hospitalView.dataSource = self;
    }
    return _hospitalView;
}

- (UITableView *)subjectView{
    if (!_subjectView) {
        _subjectView = [[UITableView alloc] init];
        [self.view insertSubview:_subjectView aboveSubview:self.bgView];
        [_subjectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(SCREEN_WIDTH / 2));
            make.top.equalTo(self.segmentView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2, 0));
        }];
        _subjectView.hidden = YES;
        _subjectView.delegate = self;
        _subjectView.dataSource = self;
    }
    return _subjectView;
}

- (NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

#pragma mark - create views
- (void)createSearchView{
    // 创建搜索框
    self.searchView = [[ByViewLookDoctorSearch alloc] init];
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    
    // 主视图
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(self.segmentView.mas_bottom);
        make.right.equalTo(@(0));
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - fetchData
- (void)fetchData{
    // 获取分院
    for (int i = 0; i < 5; i ++) {
        NSString *str = [NSString stringWithFormat:@"分院%d",i];
        [self.hospitalAry addObject:str];
    }
    
    // 获取科室
    for (int i = 0; i < 10; i ++) {
        ByModelRegistrationSubject *model = [[ByModelRegistrationSubject alloc] init];
        model.name = [NSString stringWithFormat:@"科室%d",i];
        [self.subjectAry addObject:model];
    }
    
    self.segmentView.hospital = self.hospitalAry[0];
    ByModelRegistrationSubject *first_subject = self.subjectAry[0];
    self.segmentView.subject = first_subject.name;
    
    // todo
    __weak typeof(self) weakSelf = self;
    self.segmentView.selectedSegmentBlock = ^(NSInteger segment) {
        weakSelf.selectedIndex = segment;
        if (segment == 10) {
            weakSelf.hospitalView.hidden = !weakSelf.hospitalView.hidden;
            weakSelf.bgView.hidden = weakSelf.hospitalView.hidden;
            weakSelf.subjectView.hidden = YES;
            [weakSelf.hospitalView reloadData];
        }else{
            weakSelf.subjectView.hidden = !weakSelf.subjectView.hidden;
            weakSelf.bgView.hidden = weakSelf.subjectView.hidden;
            weakSelf.hospitalView.hidden = YES;
            [weakSelf.subjectView reloadData];
        }
    };
    
    [self.hospitalView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segmentView.mas_bottom);
        make.left.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2, (self.hospitalAry.count - 1) * 30));
    }];
    
    [self.subjectView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SCREEN_WIDTH / 2));
        make.top.equalTo(self.segmentView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2, (self.subjectAry.count - 1) * 30));

    }];
    
    
    // 主数据
    for (int i = 0; i < 10; i ++) {
        ByModelRegistrationDoctor *doctor = [[ByModelRegistrationDoctor alloc] init];
        doctor.nick_name = @"博也";
        doctor.address = @"杭州 江干区";
        doctor.decribeStr = @"测试测试测试测试";
        [self.dataAry addObject:doctor];
    }
    [self.tableView reloadData];
    
}

#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == self.tableView) {
        return self.dataAry.count;
    }
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        return 1;
    }else if (tableView == self.hospitalView){
        return self.hospitalAry.count;
    }
    return self.subjectAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        ByCellLookDotor *cell = [ByCellLookDotor cellWithTableView:tableView];
        cell.model = self.dataAry[indexPath.section];
        return cell;
    }else{
        NSString *identifer = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
            cell.textLabel.font = FONT(14);
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
        }
        if (self.selectedIndex == 10) {
            cell.textLabel.text = self.hospitalAry[indexPath.row];
        }else{
            ByModelRegistrationSubject *model = self.subjectAry[indexPath.row];
            cell.textLabel.text = model.name;
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        return 85;
    }
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        ByUserIndexDoctorDetailVC *vc = [[ByUserIndexDoctorDetailVC alloc] init];
        vc.model = self.dataAry[indexPath.section];
        [self by_normalPush:vc hasNavi:YES];
    }
    
    if (tableView == self.hospitalView) {
        [self hideBgView];
        self.segmentView.hospital = self.hospitalAry[indexPath.row];
    }
    
    if (tableView == self.subjectView) {
        [self hideBgView];
        ByModelRegistrationSubject *model = self.subjectAry[indexPath.row];
        self.segmentView.subject = model.name;
    }
}

#pragma mark - 隐藏
- (void)hideBgView{
    self.bgView.hidden = YES;
    self.hospitalView.hidden = YES;
    self.subjectView.hidden = YES;
}


@end
